/*
 * jDigiClock plugin 2.1
 *
 * http://www.radoslavdimov.com/jquery-plugins/jquery-plugin-digiclock/
 *
 * Copyright (c) 2009 Radoslav Dimov
 *
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
(function($) {
    $.fn.extend({

        jdigiclock: function(options) {

            var defaults = {
                clockImagesPath: 'images/clock/',
                lang: 'en',
                am_pm: false
            };

            var regional = [];
            regional['en'] = {
                monthNames: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
                dayNames: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
            }


            var options = $.extend(defaults, options);

            return this.each(function() {
                
                var $this = $(this);
                var o = options;
                $this.clockImagesPath = o.clockImagesPath;
                $this.weatherImagesPath = o.weatherImagesPath;
                $this.lang = regional[o.lang] == undefined ? regional['en'] : regional[o.lang];
                $this.am_pm = o.am_pm;
                $this.weatherLocationCode = o.weatherLocationCode;
                $this.weatherMetric = o.weatherMetric == 'C' ? 1 : 0;
                $this.weatherUpdate = o.weatherUpdate;
                $this.currDate = '';
                $this.timeUpdate = '';


                var html = '<div id="plugin_container">';
                html    += '<div id="digital_container">';
                html    += '<div id="clock"></div>';
                html    += '</div>';
                html    += '</div>';

                $this.html(html);

                $this.displayClock($this);

                
                var panel_pos = ($('#plugin_container > div').length - 1) * 500;
               
                
            });
        }
    });
   

    $.fn.displayClock = function(el) {
        $.fn.getTime(el);
        setTimeout(function() {$.fn.displayClock(el)}, $.fn.delay());
    }

    $.fn.delay = function() {
        var now = new Date();
        var delay = (60 - now.getSeconds()) * 1000;
        
        return delay;
    }

    $.fn.getTime = function(el) {
        var now_hours, now_minutes, old_hours, old_minutes, timeOld = '';
        var start = new Date();
		var target = new Date('2014-9-4 01:55:00');
		var interval = target.getTime()-start.getTime();
		now_days = Math.floor(interval/(60*60*24*1000));
		old_days = Math.floor(interval/(60*60*24*1000));
		now_hours = Math.floor((interval/(60*60*1000))%24);
        now_minutes = Math.floor((interval/(60*1000))%60);
		old_hours = Math.floor((interval/(60*60*1000))%24);
		old_minutes = Math.floor((interval/(60*1000) + 1)%60);
		/* for testing.
		now_days = 10;
		old_days = 10;
		now_minutes = 59;
		old_minutes = 0;
		now_hours = 9;
		*/
        if(now_minutes >= 59) {
			old_hours = now_hours + 1;
			if( old_hours == '24') {
				old_hours = '00';
			}
		} else {
			old_hours = now_hours;
		}
		
		now_days    = ((now_days < 100 && now_days >= 10) ? "0" : "") + now_days;
        now_days    = ((now_days < 10) ? "00" : "") + now_days;
		now_hours   = ((now_hours < 10) ? "0" : "") + now_hours;
        now_minutes = ((now_minutes < 10) ? "0" : "") + now_minutes;
		old_days    = ((old_days < 100 && now_days >= 10) ? "0" : "") + old_days;
        old_days    = ((old_days < 10) ? "00" : "") + old_days;
		old_hours   = ((old_hours < 10) ? "0" : "") + old_hours;
        old_minutes = ((old_minutes < 10) ? "0" : "") + old_minutes;
        
        var firstHourDigit = old_hours.substr(0,1);
        var secondHourDigit = old_hours.substr(1,1);
        var firstMinuteDigit = old_minutes.substr(0,1);
        var secondMinuteDigit = old_minutes.substr(1,1);
		var firstDayDigit =  old_days.substr(0,1);
		var secondDayDigit =  old_days.substr(1,1);
		var thirdDayDigit = old_days.substr(2,1);
		
		timeOld += '<div id="days"><div class="long_line"></div>';
        timeOld += '<div id="days_bg"><img src="' + el.clockImagesPath + 'clockbg1.png" /></div>';
        timeOld += '<img src="' + el.clockImagesPath + firstDayDigit + '.png" id="fdd" class="first_digit" />';
        timeOld += '<img src="' + el.clockImagesPath + secondDayDigit + '.png" id="sdd" class="second_digit" />';
		timeOld += '<img src="' + el.clockImagesPath + thirdDayDigit + '.png" id="tdd" class="third_digit" />';
		timeOld += '</div>';
        
        timeOld += '<div id="hours"><div class="line"></div>';
        timeOld += '<div id="hours_bg"><img src="' + el.clockImagesPath + 'clockbg1.png" /></div>';
        timeOld += '<img src="' + el.clockImagesPath + firstHourDigit + '.png" id="fhd" class="first_digit" />';
        timeOld += '<img src="' + el.clockImagesPath + secondHourDigit + '.png" id="shd" class="second_digit" />';
        timeOld += '</div>';
		
        timeOld += '<div id="minutes"><div class="line"></div>';
        if (el.am_pm) {
            timeOld += '<div id="am_pm"><img src="' + el.clockImagesPath + am_pm + '.png" /></div>';
        }
        timeOld += '<div id="minutes_bg"><img src="' + el.clockImagesPath + 'clockbg1.png" /></div>';
        timeOld += '<img src="' + el.clockImagesPath + firstMinuteDigit + '.png" id="fmd" class="first_digit" />';
        timeOld += '<img src="' + el.clockImagesPath + secondMinuteDigit + '.png" id="smd" class="second_digit" />';
        timeOld += '</div>';
		
        el.find('#clock').html(timeOld);

        // set minutes
        if (old_minutes == '00') {
            firstMinuteDigit = '511'; // 0-5
        }else if (secondMinuteDigit != '9') {
			firstMinuteDigit = firstMinuteDigit + '1'; 
        }
			
		setTimeout(function() {
            $('#fmd').attr('src', el.clockImagesPath + firstMinuteDigit + '-3.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
        },200);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},250);
        setTimeout(function() {
            $('#fmd').attr('src', el.clockImagesPath + firstMinuteDigit + '-2.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
        },400);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},450);
        setTimeout(function() {
            $('#fmd').attr('src', el.clockImagesPath + firstMinuteDigit + '-1.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
        },600);

		if( secondMinuteDigit == 0 ) {
			secondMinuteDigit = '9';
		} else {
			secondMinuteDigit = secondMinuteDigit - '1';
		}
        setTimeout(function() {
			$('#smd').attr('src', el.clockImagesPath + secondMinuteDigit + '-3.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
        },200);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},250);
        setTimeout(function() {
            $('#smd').attr('src', el.clockImagesPath + secondMinuteDigit + '-2.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
        },400);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},450);
        setTimeout(function() {
            $('#smd').attr('src', el.clockImagesPath + secondMinuteDigit + '-1.png');
            $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
        },600);

        setTimeout(function() {$('#fmd').attr('src', el.clockImagesPath + now_minutes.substr(0,1) + '.png')},800);
        setTimeout(function() {$('#smd').attr('src', el.clockImagesPath + now_minutes.substr(1,1) + '.png')},800);
        setTimeout(function() { $('#minutes_bg img').attr('src', el.clockImagesPath + 'clockbg1.png')},850);

        // set hours
		if (now_minutes >= 59) {
			//alert(old_hours);
			// set hour
			if (now_hours == '23') {
				firstHourDigit = '211'; // 0-2
				secondHourDigit = '311';
			}else if (now_hours == '19') {
				firstHourDigit = '1'; // 2-1
				secondHourDigit = '9';
			}else  if (now_hours == '09') {
				firstHourDigit = '0'; // 1-0
				secondHourDigit = '9';
			}else {
				firstHourDigit = firstHourDigit + '1'; 
				if( secondHourDigit == 0 ) {
					secondHourDigit = '9';
				} else {
					secondHourDigit = secondHourDigit - '1';
				}
			}
			setTimeout(function() {
                $('#fhd').attr('src', el.clockImagesPath + firstHourDigit + '-3.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
            },200);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},250);
            setTimeout(function() {
                $('#fhd').attr('src', el.clockImagesPath + firstHourDigit + '-2.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
            },400);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},450);
            setTimeout(function() {
                $('#fhd').attr('src', el.clockImagesPath + firstHourDigit + '-1.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
            },600);

            setTimeout(function() {
            $('#shd').attr('src', el.clockImagesPath + secondHourDigit + '-3.png');
            $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg6.png');
            },200);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg5.png')},250);
            setTimeout(function() {
                $('#shd').attr('src', el.clockImagesPath + secondHourDigit + '-2.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg4.png');
            },400);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg3.png')},450);
            setTimeout(function() {
                $('#shd').attr('src', el.clockImagesPath + secondHourDigit + '-1.png');
                $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg2.png');
            },600);

            setTimeout(function() {$('#fhd').attr('src', el.clockImagesPath + now_hours.substr(0,1) + '.png')},800);
            setTimeout(function() {$('#shd').attr('src', el.clockImagesPath + now_hours.substr(1,1) + '.png')},800);
            setTimeout(function() { $('#hours_bg img').attr('src', el.clockImagesPath + 'clockbg1.png')},850);
			
        }
    }
})(jQuery);